﻿using Point2d.App;
using Point2d.Geometry;
using Point2d.Graphics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Point2d
{
    public partial class Form1 : Form
    {
        GraphicsManager _graphicsManager = new GraphicsManager();
        Lane _lane = null;
        Lane _lane2 = null;
        Vehicle _vehicle = null;
        Vehicle _vehicle2 = null;
        Video _video = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            _graphicsManager.Draw(e.Graphics);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            panel1.InitializeEx();

            _lane = new Lane(_graphicsManager, Color.Blue);
            _lane._from = new Point2(150, 500);
            _lane._to = new Point2(850, 500);
            _lane._width = 100;
            _lane.Create();
            _graphicsManager.Append(_lane);

            _lane2 = new Lane(_graphicsManager, Color.Blue);
            _lane2._from = new Point2(150, 400);
            _lane2._to = new Point2(850, 400);
            _lane2._width = 100;
            _lane2.Create();
            _graphicsManager.Append(_lane2);

            _vehicle = new Vehicle(_graphicsManager, Color.Green, _lane);
            _vehicle.Set(0.2f, 0, 0);
            _vehicle._length = 100;
            _vehicle._width = 80.0f;
            _vehicle.Update();
            _graphicsManager.Append(_vehicle);

            _vehicle2 = new Vehicle(_graphicsManager, Color.Red, _lane);
            _vehicle2.Set(0.8f, 0, 0);
            _vehicle2._length = 200;
            _vehicle2._width = 80.0f;
            _vehicle2.Update();
            _graphicsManager.Append(_vehicle2);

            _video = new Video(_vehicle);

            ListViewItem item = listView1.Items.Add(_vehicle._status._pos.ToString());
            item.SubItems.Add(_vehicle._status._angle.ToString());
            item.SubItems.Add(_vehicle._status._posLat.ToString());

            UpdateUI(_vehicle._status._pos, _vehicle._status._angle, _vehicle._status._posLat);
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            _graphicsManager._viewControl.Draging(e.Location);

            panel1.Invalidate();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            _graphicsManager._viewControl.Start(e.Location);

            panel1.Invalidate();
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            _graphicsManager._viewControl.End();

            panel1.Invalidate();
        }

        private void panel1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                _graphicsManager.Scale(1.1f, panel1.Width, panel1.Height);
            }
            else
            {
                _graphicsManager.Scale(0.9f, panel1.Width, panel1.Height);
            }

            panel1.Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog1.FileName;

                StreamReader xmlReader = new StreamReader(fileName, Encoding.UTF8);
                string xml = xmlReader.ReadToEnd();
                xmlReader.Dispose();
                xmlReader.Close();
                xmlReader = null;

                richTextBox1.Text = xml;

                _graphicsManager._dataSupplier.Load(richTextBox1.Text);
                _graphicsManager._dataSupplier.Parse(_graphicsManager);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            _vehicle.Rotate(1);
            UpdateUI(_vehicle._status._pos, _vehicle._status._angle, _vehicle._status._posLat);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            _vehicle.Rotate(-1);
            UpdateUI(_vehicle._status._pos, _vehicle._status._angle, _vehicle._status._posLat);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            _vehicle.RelativeMove(0.01f);
            UpdateUI(_vehicle._status._pos, _vehicle._status._angle, _vehicle._status._posLat);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            _vehicle.RelativeMove(-0.01f);
            UpdateUI(_vehicle._status._pos, _vehicle._status._angle, _vehicle._status._posLat);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            _vehicle.PosMove(0.01f);
            UpdateUI(_vehicle._status._pos, _vehicle._status._angle, _vehicle._status._posLat);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            _vehicle.PosMove(-0.001f);
            UpdateUI(_vehicle._status._pos, _vehicle._status._angle, _vehicle._status._posLat);
        }

       
        private void button7_Click(object sender, EventArgs e)
        {
            ListViewItem item = listView1.Items.Add(_vehicle._status._pos.ToString());
            item.SubItems.Add(_vehicle._status._angle.ToString());
            item.SubItems.Add(_vehicle._status._posLat.ToString());
        }

        private void button10_Click(object sender, EventArgs e)
        {
            _video.Clear();

            for (int i = 0; i < listView1.Items.Count; i ++)
            {
                _video.AppendItem(float.Parse(listView1.Items[i].SubItems[0].Text),
                    float.Parse(listView1.Items[i].SubItems[1].Text),
                    float.Parse(listView1.Items[i].SubItems[2].Text));
            }

            _video.Create();
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            bool ok = _video.Play();
            panel1.Invalidate();
            if (ok == false)
            {
                timer1.Stop();
            }
        }

        void UpdateUI(float pos, float angle, float posLat)
        {
            label1.Text = string.Format("{0:0.00}", posLat);
            label2.Text = string.Format("{0:0.00}", angle);
            label3.Text = string.Format("{0:0.00}", pos);
            panel1.Invalidate();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (FileStream stream = File.OpenWrite(saveFileDialog1.FileName))
                {
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        for (int i = 0; i < listView1.Items.Count; i++)
                        {
                            writer.WriteLine(
                                string.Format("{0},{1},{2}",
                                listView1.Items[i].SubItems[0].Text,
                                listView1.Items[i].SubItems[1].Text,
                                listView1.Items[i].SubItems[2].Text));
                        }

                        writer.Close();
                    }
                }
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog1.FileName;

                StreamReader xmlReader = new StreamReader(fileName, Encoding.UTF8);
                string xml = xmlReader.ReadToEnd();
                xmlReader.Dispose();
                xmlReader.Close();
                xmlReader = null;

                listView1.Items.Clear();
                string[] lines = xml.Split('\n');
                foreach (var it in lines)
                {
                    if (it == string.Empty)
                        continue;

                    string[] data = it.Split(',');
                    ListViewItem item = listView1.Items.Add(data[0]);
                    item.SubItems.Add(data[1]);
                    item.SubItems.Add(data[2]);
                }

                _video.Clear();
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    _video.AppendItem(float.Parse(listView1.Items[i].SubItems[0].Text),
                        float.Parse(listView1.Items[i].SubItems[1].Text),
                        float.Parse(listView1.Items[i].SubItems[2].Text));
                }
                _video.Create();
            }
        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (listView1.SelectedIndices.Count <= 0)
                return;

            int sel = listView1.SelectedIndices[0];
            int next = sel + 1;
            if (next >= listView1.Items.Count)
                return;

            _video.CreateSubItem(sel);
            timer1.Start();
        }
    }
}
